<?php
/**
 * User: 1971229701@qq.com
 * Date: 2020/11/4/004
 * Time: 10:24
 */

namespace app\admin\controller;


use app\admin\logic\CateLogic;
use think\Request;

class Cate extends BaseAdmin
{
    public function listing()
    {
        return $this->fetch();
    }

    public function ajax()
    {
        $limit = $this->_get('limit', 10);
        $r = (new CateLogic())->listing($limit);
        return $r;
    }

    public function create(Request $request)
    {
        if ($request->isPost()) {
            $data = $request->post();
            $logic = new CateLogic();
            if ($logic->create($data) === false) {
                return apiReturnFail($logic->getError());
            }
            return apiReturnSuc();
        } else {
            return $this->fetch();
        }
    }

    public function update(Request $request)
    {

        $logic = new CateLogic();
        if ($request->isPost()) {
            $data = $request->post();
            if ($logic->update($data) === false) {
                return apiReturnFail($logic->getError());
            }
            return apiReturnSuc();
        } else {

            $id = $this->_get('id');
            $r = $logic->update($id, 'get');
            $this->assign('info', $r);
            return $this->fetch();
        }
    }

    public function delete($id)
    {
        $r = (new CateLogic())->delete($id);
        return apiReturnSuc($r);
    }
}